#!/bin/sh /etc/rc.common
START=99
STOP=10
USE_PROCD=1
PROG="/xunyou/xunyou_daemon.sh"

get_config() {
	config_get_bool enabled $1 enabled 1
	config_get XUNYOU_IFACE "$1" interface "lan"
}

get_interface() {
	network_get_device ifname "$1" || ifname="br-lan"
}

start_service() {
	config_load xunyou
	config_foreach get_config xunyou
	
	if [ $enabled == 1 ]; then
		local interface=$(
			. /lib/functions/network.sh
			network_is_up "$XUNYOU_IFACE" || exit 0
			network_get_device device "$XUNYOU_IFACE"
			printf "%s" "${device}"
		)
		[ -z "$interface" ] && interface=br-lan
		procd_open_instance
		procd_set_param command "$PROG" restart -i "$interface"
		procd_set_param stdout 1
		procd_set_param stderr 1
		procd_close_instance
	else
		sh /xunyou/xunyou_daemon.sh stop >/dev/null 2>&1
	fi
}

service_triggers() {
	procd_add_reload_trigger "xunyou"
}
